﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace chemicall
{
    public class MenuScreen : GameScreen
    {
        private List<MenuItem> menuEntries = new List<MenuItem>();
        private int selectedEntry = 0;
        private string menuTitle;
        private KeyboardState keyPressed;
        private Texture2D backgroundTexture;
        private Keys lastPressed;
        
        protected IList<MenuItem> MenuEntries
        {
            get { return menuEntries; }
        }

        public MenuScreen(string menuTitle)
        {
            this.menuTitle = menuTitle;
        }

        public override void Activate(bool instancePreserved)
        {
            if (!instancePreserved)
            {
                if (content == null)
                    content = new ContentManager(ScreenManager.Game.Services, "Content");

                backgroundTexture = content.Load<Texture2D>("menuBackground");
            }
        }

        public override void HandleInput(GameTime gameTime, KeyboardState state)
        {
            keyPressed = state;
            if (keyPressed.IsKeyUp(Keys.Up) && lastPressed.Equals(Keys.Up))
            {
                SoundManager.getInstance().startEvent(CONSTANTES.MENU_SOUND_ID);
                selectedEntry--;
                if (selectedEntry < 0)
                    selectedEntry = menuEntries.Count-1;
            }
            else if (keyPressed.IsKeyUp(Keys.Down) && lastPressed.Equals(Keys.Down))
            {
                SoundManager.getInstance().startEvent(CONSTANTES.MENU_SOUND_ID);
                selectedEntry++;
                if (selectedEntry >= menuEntries.Count)
                    selectedEntry = 0;
            }
            else if (keyPressed.IsKeyUp(Keys.Enter) && lastPressed.Equals(Keys.Enter))
                OnSelectEntry(selectedEntry, PlayerIndex.One);

            if (keyPressed.GetPressedKeys().GetLength(0) > 0)
                lastPressed = keyPressed.GetPressedKeys()[0];
            else
                lastPressed = Keys.None;
        }

        protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
        {
            if(0<= entryIndex && entryIndex < menuEntries.Count)
                menuEntries[entryIndex].OnSelectEntry(playerIndex);
        }

        protected virtual void OnCancel(PlayerIndex playerIndex)
        {
            ExitScreen();
        }

        protected void OnCancel(object sender, System.EventArgs e)
        {
            OnCancel(PlayerIndex.One);
        }

        protected virtual void UpdateMenuEntryLocations()
        {
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            Vector2 position = new Vector2(0f, 175f);
            for (int i = 0; i < menuEntries.Count; i++)
            {
                MenuItem menuEntry = menuEntries[i];

                position.X = ScreenManager.GraphicsDevice.Viewport.Width / 2 - menuEntry.GetWidth(this) / 2;

                if (ScreenState == ScreenState.TransitionOn)
                    position.X -= transitionOffset * 256;
                else
                    position.X += transitionOffset * 512;
                menuEntry.Position = position;
                position.Y += menuEntry.GetHeight(this);
            }
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            for (int i = 0; i < menuEntries.Count; i++)
            {
                bool isSelected = IsActive && (i == selectedEntry);
                menuEntries[i].Update(this, isSelected, gameTime);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            UpdateMenuEntryLocations();

            GraphicsDevice graphics = ScreenManager.GraphicsDevice;
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);

            spriteBatch.Begin();
            if (backgroundTexture != null)
                spriteBatch.Draw(backgroundTexture, fullscreen,
                             new Color(TransitionAlpha, TransitionAlpha, TransitionAlpha));

            for (int i = 0; i < menuEntries.Count; i++)
            {
                MenuItem menuEntry = menuEntries[i];

                bool isSelected = IsActive && (i == selectedEntry);

                menuEntry.Draw(this, isSelected, gameTime);
            }

            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 80);
            Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
            Color titleColor = new Color(192, 192, 192) * TransitionAlpha;
            float titleScale = 1.25f;

            titlePosition.Y -= transitionOffset * 100;

            spriteBatch.DrawString(font, menuTitle, titlePosition, Color.Blue, 0,
                                   titleOrigin, titleScale, SpriteEffects.None, 0);

            spriteBatch.End();

        }
    }
}
